import os
import sys

import bpy
from bpy.types import NodeTree, Node, NodeSocket
import nodeitems_utils
from nodeitems_utils import NodeCategory, NodeItem

pwd = os.getcwd()
sys.path.append(pwd)

from network_data import *
from blender_additional_function import *


'''
draw object of vad graph
'''
class DrawVads(object, metaclass=Singleton):

    def __init__(self):
        self.imageNum = 0
        self.vadGraphs = []
        # node color
        self.colors = {"red": (1.0, 0.411, 0.503), "yellow": (0.981, 1.0, 0.272), "green": (0.563, 1.0, 0.541),
                       "blue": (0.241, 0.861, 1.0), "gray": (0.4, 0.4, 0.4), "white": (1.0, 1.0, 1.0),
                       "purple": (0.58, 0.579, 1.0)}

    def drawVadNode(self, vadtree, tree):
        # create input vad node
        for vad_label in vadtree["nodes"]:
            vad_node = tree.nodes.new(type='VadNodeType')
            vad_node.location = tuple(vadtree["nodes"][vad_label]["location"])
            vad_node.name = vad_label
            vad_node.label = vad_label
            vad_node.VAD = vadtree["nodes"][vad_label]["address"]
            vad_node.use_custom_color = True
            vad_node.color = self.colors[vadtree["nodes"][vad_label]["color"]]

        links = tree.links
        for e in vadtree["edges"]:
            source = tree.nodes[e["source"]]
            target = tree.nodes[e["target"]]
            links.new(source.outputs[0], target.inputs[0])


class VadCustomTree(NodeTree):
    '''A vad node tree type that will show up in the node editor header'''
    bl_idname = 'VadTreeType'
    bl_label = 'Vad Node Tree'
    bl_icon = 'NODETREE'


# Vad socket type
class VadCustomSocket(NodeSocket):
    '''Vad node socket type'''
    bl_idname = 'VadSocketType'
    bl_label = 'Vad Node Socket'

    def draw(self, context, layout, node, text):
        layout.label(text)

    def draw_color(self, context, node):
        return (1.0, 0.4, 0.216, 0.5)


class VadCustomTreeNode:
    @classmethod
    def poll(cls, ntree):
        return ntree.bl_idname == 'VadTreeType'


class VadCustomNode(Node, VadCustomTreeNode):
    '''A custom node'''
    bl_idname = 'VadNodeType'
    bl_label = 'Vad Node'
    bl_icon = 'SOUND'

    VAD = bpy.props.StringProperty(default="0x70238a30 - 0x7ffd2fff")

    def init(self, context):
        self.inputs.new('VadSocketType', " ")

        self.outputs.new('NodeSocketColor', " ")
    
    def copy(self, node):
        pass
    
    def free(self):
        pass
    
    def draw_buttons(self, context, layout):
        tmp = self.VAD
        layout.label(text=str(tmp))
    
    def draw_label(self):
        return "vad node"

